﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IsmsExplorer.Infrastructure;
using IsmsExplorer.Model.RiskRegister;
using IsmsExplorer.Infrastructure.RepositoryFramework;
using IsmsExplorer.Model.Iso27001;
using IsmsExplorer.Model.AssetRegister;
using System.Diagnostics;

namespace IsmsExplorer.Application.RiskRegister {
    public static class RiskRegisterService {
        private static IUnitOfWork _unitOfWork;
        private static IThreatRepository _threatRepository;
        private static ITreatmentRepository _treatmentRepository;

        static RiskRegisterService() {
            _unitOfWork = new UnitOfWork();
            _threatRepository = RepositoryFactory.GetRepository<IThreatRepository, Threat>(_unitOfWork);
            _treatmentRepository = RepositoryFactory.GetRepository<ITreatmentRepository, Treatment>(_unitOfWork);
        }

        public static void SaveThreat(Threat threat) {
            _threatRepository[threat.ID] = threat;
            _unitOfWork.Commit();
        }

        public static void SaveTreatment(Treatment treatment) {
            _treatmentRepository[treatment.ID] = treatment;
            _unitOfWork.Commit();
        }

        /// <summary>
        /// Save Threat and corresponding Treatment in one transaction.
        /// </summary>
        /// <param name="threat"></param>
        public static void SaveThreatAndTreatment(Threat threat) {
            Debug.Assert(threat != null && threat.Treatment != null);
            if (threat != null && threat.Treatment != null) {
                _threatRepository[threat.ID] = threat;
                _treatmentRepository[threat.Treatment.ID] = threat.Treatment;
                _unitOfWork.Commit();
            }
        }

        /// <summary>
        /// Deletes the Threat and associated Treatment
        /// NOTE: associated Treatment is deleted because of database CASCADE deletion.
        /// </summary>
        /// <param name="threat"></param>
        public static void DeleteThreatAndTreatment(Threat threat) {
            if (threat != null) {
                _threatRepository.Remove(threat);
                _unitOfWork.Commit();
            }
        }

        public static Treatment GetTreatmentForThreat(Threat threat) {
            return _treatmentRepository.FindBy(threat);
        }

        public static void AssociateIsoControls(Treatment treatment, IList<Iso27001Control> controls) {
            _treatmentRepository.AssociateIsoControls(treatment, controls);
            _unitOfWork.Commit();
        }

        public static void DisassociateIsoControls(Treatment treatment, IList<Iso27001Control> controls) {
            _treatmentRepository.DisassociateIsoControls(treatment, controls);
            _unitOfWork.Commit();
        }

        public static IList<Threat> GetThreatsForAsset(Asset asset) {
            return _threatRepository.FindBy(asset);
        }

    } // end public static class RiskRegisterService

}
